Public Class frmTreeView

    Public Property SelectedItem() As String
        Get
            If TreeView1.SelectedNode Is Nothing Then Return "" Else Return TreeView1.SelectedNode.Text
        End Get
        Set(ByVal value As String)
            SelectItem(TreeView1, value)
        End Set
    End Property

    Private Function SelectItem(ByVal ParentNode As Object, ByVal Value As String) As Boolean
        For Each tn As TreeNode In ParentNode.Nodes
            If tn.Text = Value Then
                TreeView1.SelectedNode = tn
                Return True
            End If
            If SelectItem(tn, Value) Then Return True
        Next
    End Function

    Private Sub frmTreeView_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Chr(27) Then
            SelectedItem = ""
            DialogResult = Windows.Forms.DialogResult.Cancel
            Close()
        ElseIf e.KeyChar = Chr(13) Then
            If TreeView1.SelectedNode.Nodes.Count = 0 Then Exit Sub
            SelectedItem = TreeView1.SelectedNode.Text
            DialogResult = Windows.Forms.DialogResult.OK
            Close()
        End If
    End Sub

    Public Sub New(ByVal TreeViewList As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        FillTreeView(TreeView1, TreeViewList)
    End Sub

    Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        If e.Node.Nodes.Count = 0 Then
            SelectedItem = TreeView1.SelectedNode.Text
            DialogResult = Windows.Forms.DialogResult.OK
            Close()
        End If
    End Sub
End Class